package dao.impl;

import dao.FavouriteDao;
import pojo.Favourite;
import util.DataUtils;
import util.JdbcUtils;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class FavouriteDaoImpl implements FavouriteDao {
    private Connection conn = null;

    private ResultSet rs = null;
    private boolean flag = false;

    public ArrayList<Favourite> favouriteList(Integer uid) throws SQLException, ClassNotFoundException {
        conn = JdbcUtils.dbconn();
        ArrayList<Favourite> favouriteList = new ArrayList<>();
        rs = JdbcUtils.selectsql("select * from favourite where uid='" + uid + "'");
//        while (true) {
//                if (!rs.next()) break;
//                Favourite favourite = new Favourite();
//                favourite.setNid(rs.getInt("nid"));
//                favourite.setUid(rs.getInt("uid"));
//                favouriteList.add(favourite);
//        }
        rs.last();
        int row = rs.getRow();
        rs.beforeFirst();
        do {
            row--;
            Favourite favourite = new Favourite();
            favourite = (Favourite) DataUtils.dataEncapsulation(Class.forName("pojo.Favourite"), rs);
            favouriteList.add(favourite);
        } while (row > 0);
        JdbcUtils.closeconn(rs, null, conn);
        return favouriteList;
    }

    public boolean UserFavouriteNote(Integer uid, Integer nid) throws SQLException {
        conn = JdbcUtils.dbconn();
        Integer i = 0;
        String sql = "insert into favourite values('" + nid + "','" + uid + "')";
        String sql2 = "update biji set favourite=favourite+1 where nid='" + nid + "'";
        Integer j = JdbcUtils.addsql(sql2);
        i = JdbcUtils.addsql(sql);
        if (i > 0 && j > 0) flag = true;
        else flag = false;
        JdbcUtils.closeconn(null, null, conn);
        return flag;
    }


    public Integer noteFavouriteTotal(Integer nid) throws SQLException {
        Integer number = 0;
        conn = JdbcUtils.dbconn();
        rs = JdbcUtils.selectsql("select favourite from biji where nid='" + nid + "'");
        while (true) {
            if (!rs.next()) break;
            number = rs.getInt("favourite");
        }
        JdbcUtils.closeconn(rs, null, conn);
        return number;
    }

    public boolean findFavourite(Integer uid, Integer nid) throws SQLException {
        conn = JdbcUtils.dbconn();
        rs = JdbcUtils.selectsql("select * from favourite where uid='" + uid + "' and nid='" + nid + "'");
        while (true) {
            if (!rs.next()) break;
            flag = true;
        }
        JdbcUtils.closeconn(rs, null, conn);
        return flag;
    }

    public boolean UserUnFavourite(Integer uid, Integer nid) throws SQLException {
        conn = JdbcUtils.dbconn();
        Integer i = 0;
        String sql = "delete from favourite where nid='" + nid + "' and uid='" + uid + "'";
        String sql2 = "update biji set favourite=favourite-1 where nid='" + nid + "'";
        Integer j = JdbcUtils.addsql(sql2);
        i = JdbcUtils.addsql(sql);
        if (i > 0 && j > 0) flag = true;
        JdbcUtils.closeconn(null, null, conn);
        return flag;
    }
}
